package org.meetunexpectedly.entity.video;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.meetunexpectedly.config.QiNiuConfig;
import org.meetunexpectedly.entity.BaseEntity;
import org.meetunexpectedly.entity.vo.UserVO;
import org.springframework.util.ObjectUtils;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 视频数据库表
 * @Author: pupil
 * @Date: 2024/08/17 下午 9:21
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class Video extends BaseEntity {

    private static final long serialVersionUID = 1L;

    // YV ID 以YV+UUID 视频云存储id
    private String yv;

    // 标题
    @NotBlank(message = "标题不能为空")
    private String title;

    // 描述
    private String description;

    // 视频源（文件表id），视频很多详细内容存储在文件表中，用于资源保护
    private Long url;

    // 发布者id（用户id）
    private Long userId;

    // 分类id
    private Long typeId;

    // 封面源
    private Long cover;

    // 公开/私密，0：公开，1：私密，默认为0
    private Boolean open;

    // 审核状态:通过,未通过,审核中
    private Integer auditStatus;

    // 审核状态的消息
    private String msg;

    // 审核快慢状态 0：快速  1：慢速
    private Boolean auditQueueStatus;

    // 点赞数
    private Long thumbCount;

    // 分享数
    private Long shareCount;

    // 浏览次数
    private Long historyCount;

    // 收藏次数
    private Long favoritesCount;

    // 评论次数
    private Long commentCount;

    // 视频时长
    private String duration;

    // 标签
    private String labelNames;

    // 视频分类
    @TableField(exist = false)
    private String videoType;

    // 封面分类
    /*@TableField(exist = false)
    private String coverType;*/

    // 关联的用户
    @TableField(exist = false)
    private UserVO user;

    // 关联分类名称
    @TableField(exist = false)
    private String typeName;

    // 是否点赞
    @TableField(exist = false)
    private Boolean thumb;

    // 是否收藏
    @TableField(exist = false)
    private Boolean favorites;

    // 是否关注
    @TableField(exist = false)
    private Boolean follow;

    // 用户昵称
    @TableField(exist = false)
    private String userName;

    // 审核状态名称
    @TableField(exist = false)
    private String auditStateName;

    // 是否公开
    @TableField(exist = false)
    private String openName;

    /**
     * 获取标签名
     *
     * @return
     */
    public List<String> buildLabel() {
        if (ObjectUtils.isEmpty(this.labelNames)) return Collections.EMPTY_LIST;
        return Arrays.asList(this.labelNames.split(","));
    }

    /**
     * 获取视频源完整地址
     *
     * @return
     */
    public String buildVideoUrl() {
        return QiNiuConfig.RealmName + "/" + this.url;
    }

    /**
     * 获取封面源完整地址
     *
     * @return
     */
    public String buildCoverUrl() {
        return QiNiuConfig.RealmName + "/" + this.cover;
    }

    /**
     * 设置视频审核消息，拼接多个信息
     * @param msg
     */
    public void setMessage(String msg) {
        String oldMsg = this.getMsg();
        if (!ObjectUtils.isEmpty(oldMsg) || oldMsg.equals("")) {
            this.setMsg(msg);
        } else {
            this.setMsg(oldMsg + "\n" + msg);
        }
    }
}
